package fun.yizhierha.tools.other.controller;

import fun.yizhierha.common.annotation.Log;
import fun.yizhierha.common.base.BaseErrDto;
import fun.yizhierha.common.config.RsaEncryptConfig;
import fun.yizhierha.common.exception.BizCodeEnum;
import fun.yizhierha.common.utils.ValidUtils;
import fun.yizhierha.common.utils.*;
import fun.yizhierha.tools.other.domain.ToolQiniuConfig;
import fun.yizhierha.tools.other.domain.vo.CreateToolQiniuConfigVo;
import fun.yizhierha.tools.other.domain.vo.UpdateToolQiniuConfigVo;
import fun.yizhierha.tools.other.domain.vo.RetrieveToolQiniuConfigVo;

import fun.yizhierha.tools.other.service.ToolQiniuConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import lombok.RequiredArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Set;

/** generated by EH-Admin
* @author erha
* @date Sun Jan 08 13:57:58 CST 2023
**/
@Api(tags = "系统管理:存储管理:七牛云配置")
@RestController
@RequestMapping("/api/tools/storage/qiniuConfig")
@RequiredArgsConstructor
public class ToolQiniuConfigController{

    private final ToolQiniuConfigService toolQiniuConfigService;
    private final RsaEncryptConfig rsaEncryptConfig;

    @ApiOperation("获取七牛云配置")
    @Log("获取七牛云配置")
    @GetMapping
    @PreAuthorize("@eh.check('tools:storage:qiniuConfig:list')")
    public R<PageUtils<ToolQiniuConfig>> list(RetrieveToolQiniuConfigVo retrieveToolQiniuConfigVo, Query.PageVo pageVo){
        PageUtils<ToolQiniuConfig> res = toolQiniuConfigService.list(retrieveToolQiniuConfigVo,pageVo);
        return R.<PageUtils<ToolQiniuConfig>>ok().setData(res);
    }

    @ApiOperation("新增七牛云配置")
    @Log("新增七牛云配置")
    @PostMapping
    @PreAuthorize("@eh.check('tools:storage:qiniuConfig:add')")
    public R<List<BaseErrDto>> add(@Validated @RequestBody CreateToolQiniuConfigVo createToolQiniuConfigVo,
        BindingResult bindingResult) throws Exception {
        List<BaseErrDto> errDtoList = ValidUtils.getBaseErrDtoByBindingRes(bindingResult);
        if (errDtoList.isEmpty()){
            // sk,ak解密
            String ak = EncryptUtils.rsaDecryptByPrivateKey(rsaEncryptConfig.getPrivateKey(), createToolQiniuConfigVo.getAccessKey());
            String sk = EncryptUtils.rsaDecryptByPrivateKey(rsaEncryptConfig.getPrivateKey(), createToolQiniuConfigVo.getSecretKey());
            createToolQiniuConfigVo.setAccessKey(ak);
            createToolQiniuConfigVo.setSecretKey(sk);
            toolQiniuConfigService.save(createToolQiniuConfigVo);
            return R.ok();
        }
        return R.<List<BaseErrDto>>error(
                BizCodeEnum.Client_Error_CRUD.getCode(), BizCodeEnum.Client_Error_CRUD.getMsg()
        ).setData(errDtoList);
    }

    @ApiOperation("修改七牛云配置")
    @Log("修改七牛云配置")
    @PutMapping
    @PreAuthorize("@eh.check('tools:storage:qiniuConfig:edit')")
    public R<List<BaseErrDto>> edit(@Validated @RequestBody ValidList<UpdateToolQiniuConfigVo> updateToolQiniuConfigList,
                    BindingResult bindingResult){
        List<BaseErrDto> errDtoList = ValidUtils.getBaseErrDtoByBindingRes(updateToolQiniuConfigList, bindingResult);
        if (errDtoList.isEmpty()) {
            toolQiniuConfigService.edit(updateToolQiniuConfigList,errDtoList);
            if (errDtoList.isEmpty()){
                return R.ok();
            }else {
                return  R.<List<BaseErrDto>>error(
                        BizCodeEnum.Client_Error_CRUD.getCode(),
                        BizCodeEnum.Client_Error_CRUD.getMsg()
                ).setData(errDtoList);
            }
        }
        return  R.<List<BaseErrDto>>error(
                BizCodeEnum.Client_Error_CRUD.getCode(),
                BizCodeEnum.Client_Error_CRUD.getMsg()
        ).setData(errDtoList);
    }

    @ApiOperation("激活当前配置")
    @Log("激活当前配置")
    @PostMapping("active/{id}")
    @PreAuthorize("@eh.check('tools:storage:qiniuConfig:edit')")
    public R active(@PathVariable Long id){
        toolQiniuConfigService.activeConfig(id);
        return R.ok();
    }

    @ApiOperation("删除七牛云配置")
    @Log("删除七牛云配置")
    @DeleteMapping
    @PreAuthorize("@eh.check('tools:storage:qiniuConfig:del')")
    public R del(@RequestBody Set<Long> ids){
        toolQiniuConfigService.remove(ids);
        return R.ok();
    }

    @ApiOperation("导出数据")
    @Log("导出数据")
    @GetMapping("/download")
    @PreAuthorize("@eh.check('tools:storage:qiniuConfig:list')")
    public void download(HttpServletResponse response){
        toolQiniuConfigService.download(response);
    }

}